package com.horoscope.astrology.zodiac.signs.free.utils.dialog

import android.content.Context
import android.content.DialogInterface
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.horoscope.astrology.zodiac.signs.free.R
import com.horoscope.astrology.zodiac.signs.free.adapter.CityAdapter
import com.horoscope.astrology.zodiac.signs.free.bean.CityInfo
import com.horoscope.astrology.zodiac.signs.free.bean.CityResultBean
import com.horoscope.astrology.zodiac.signs.free.network.HttpEngine
import com.horoscope.astrology.zodiac.signs.free.network.NetworkEngine
import com.horoscope.astrology.zodiac.signs.free.network.NetworkListener

class DialogSelectCity(private val city: String?) : BaseDialogFragment() {

    private lateinit var adapter: CityAdapter
    private lateinit var mRecyclerView: RecyclerView
    private lateinit var mEditText: EditText

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.dialog_select_city, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mEditText = view.findViewById(R.id.edit_text_input)
        mEditText.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable?) {
            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                if (s.toString().length >= 2) {
                    request(s.toString())
                }
            }

        })

        mRecyclerView = view.findViewById(R.id.recycler_city)
        val layoutManager = LinearLayoutManager(activity)
        layoutManager.orientation = LinearLayoutManager.VERTICAL
        mRecyclerView.layoutManager = layoutManager
        adapter =
            CityAdapter(activity!!)
        mRecyclerView.adapter = adapter
        adapter.setClickListener(object : CityAdapter.ICityOnClick {
            override fun onCityClick(bean: CityInfo) {
                mListener?.onCityClick(bean)
                dismiss()
            }
        })
        if (city.isNullOrBlank()) {
            request("paris")
        } else {
            mEditText.hint = city
            request(city)
        }
        mEditText.postDelayed({
            showInput()
        }, 0)
    }

    private fun showInput() {
        showInput(mEditText)
    }

    private fun showInput(et: EditText) {
        et.requestFocus()
        val imm = activity?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.showSoftInput(et, InputMethodManager.SHOW_IMPLICIT)
    }


    fun request(startWith: String) {
        HttpEngine.setCityUrl()
        NetworkEngine.getCityResult(startWith, object : NetworkListener<CityResultBean>() {
            override fun onSuccess(t: CityResultBean) {
                if (isAdded) {
                    adapter.setData(t.geonames)
                }
                HttpEngine.setBASEUrl()
            }

            override fun onFailed(t: CityResultBean) {
                super.onFailed(t)
                if (isAdded) {
                    adapter.setData(t.geonames)
                }
                HttpEngine.setBASEUrl()
            }

            override fun onError(e: String?) {
                super.onError(e)
                HttpEngine.setBASEUrl()
            }

        })
    }


    private var mListener: ICityOnClick? = null
    fun setClickListener(listener: ICityOnClick) {
        mListener = listener
    }

    interface ICityOnClick {
        fun onCityClick(bean: CityInfo)
    }

    private var mDialogInterface: DialogInterface.OnDismissListener? = null
    fun setOnDismissListener(listener: DialogInterface.OnDismissListener) {
        mDialogInterface = listener
    }

    override fun onDismiss(dialog: DialogInterface) {
        super.onDismiss(dialog)
        if (mDialogInterface != null) {
            mDialogInterface!!.onDismiss(dialog)
        }
    }
}